package cn.nubia.admin.common.ftp;

import org.apache.commons.lang.StringUtils;

/**
 * Created by wangj on 2015/6/26.
 */
public class FtpHelper {
    public static final String FILE_DIRECTORY_SEPARATOR = "/";

    /**
     * 使用带文件名来生成路径
     *
     * @param filename
     * @param paths
     * @return 例fileName:index.jsp    paths:a   /b/  c  d/
     * 生成  /a/b/c/d/index.jsp
     */
    public static String generatePathWithFilename(String filename, String... paths) {
        StringBuilder sb = new StringBuilder(FILE_DIRECTORY_SEPARATOR);

        for (String path : paths) {
            if (StringUtils.isEmpty(path)) {
                continue;
            }

            sb.append(removeSlash(path)).append(FILE_DIRECTORY_SEPARATOR);
        }
        sb.append(removeSlash(filename));
        return sb.toString();
    }

    private static String removeSlash(String path) {
        int start = 0;
        int end = path.length();
        if (path.startsWith(FILE_DIRECTORY_SEPARATOR)) {
            start = 1;
        }
        if (path.endsWith(FILE_DIRECTORY_SEPARATOR) && end > 1) {
            end = end - 1;
        }
        return path.substring(start, end);
    }

}
